package com.KKB.dao.impl;

import com.KKB.bean.Menu;
import com.KKB.dao.MenuDao;
import com.KKB.util.DBUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MenuDaoImpl extends DBUtils implements MenuDao {
    @Override
    public List<Menu> getMenuList() {
        List<Menu> menuList=new ArrayList<>();
        try {
            String sql="select * from menu";

            resultSet= query(sql,null);
            while (resultSet.next()){
                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUpmenuID(resultSet.getInt("upmenuid"));
                menu.setState(resultSet.getInt("state"));
                menu.setDesc(resultSet.getString("desc"));
                menu.setUrl(resultSet.getString("url"));
                menuList.add(menu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }

        return menuList;
    }

    @Override
    public int total() {
        int count = 0;
        try {
            String sql = " select count(*) from menu ";
            query(sql, null);
            while (resultSet.next()) {
                count = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return count;
    }

    @Override
    public List<Menu> getMenuList(int pageIndex, int pageSize) {
        List<Menu> menuList = new ArrayList<>();
        try {
            String sql = " select * from menu limit ?,? ";
            List params = new ArrayList();
            params.add((pageIndex-1)*pageSize);
            params.add(pageSize);
            query(sql, params);
            while (resultSet.next()) {
                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUpmenuID(resultSet.getInt("upmenuid"));
                menu.setState(resultSet.getInt("state"));
                menu.setUrl(resultSet.getString("url"));
                menu.setDesc(resultSet.getString("desc"));
                menuList.add(menu);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return menuList;
    }

    @Override
    public Menu findById(int menuId) {
        Menu menu = new Menu();
        try {
            String sql = " select * from menu where menuid=? ";
            List params = new ArrayList();
            params.add(menuId);
            query(sql, params);
            while (resultSet.next()) {
                menu.setMenuId(menuId);
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUpmenuID(resultSet.getInt("upmenuid"));
                menu.setState(resultSet.getInt("state"));
                menu.setUrl(resultSet.getString("url"));
                menu.setDesc(resultSet.getString("desc"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return menu;
    }

    @Override
    public int insert(Menu menu) {
        int update = 0;
        try {
            String sql = "insert into menu values(null,?,?,?,?,?) ";
            List params = new ArrayList();

            params.add(menu.getMenuName());
            params.add(menu.getUpmenuID());
            params.add(menu.getState());
            params.add(menu.getUrl());
            params.add(menu.getDesc());
            update = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }

    @Override
    public int deleteMenu(int menuId) {
        int update = 0;
        try {
            String sql = "delete from menu where menuid=? ";
            List params = new ArrayList();
            params.add(menuId);
            update = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }

    @Override
    public int updateMenu(Menu menu) {
        int update = 0;
        try {
            String sql = " update menu set menuname=?,upmenuid=?,state=?,url=?,`desc`=? where menuid=? ";
            List params = new ArrayList();
            params.add(menu.getMenuName());
            params.add(menu.getUpmenuID());
            params.add(menu.getState());
            params.add(menu.getUrl());
            params.add(menu.getDesc());
            params.add(menu.getMenuId());
            update = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }

}
